package util;

import com.alibaba.fastjson.JSONArray;

import java.util.List;

public class Condition {
    public static String toWhereClause(List<Condition> condition) {

        //以WHERE开头，注意前后的空格
        StringBuilder sb = new StringBuilder(" WHERE ");
        //将字段名，操作符和字段值依次拼接为字串
        //每个条件后面都附加上“ABD",注意前后的空格
        for (Condition c : condition) {
            sb.append(c.getKey()).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            //如果操作符是like，自动设置为模糊匹配
            if ("like".equalsIgnoreCase(c.getOperator())) {
                value = "%" + value + "%";
            }
            value = StringHelper.quoteMarked(value);
            sb.append(value);
            sb.append(" AND ");
        }
        StringHelper.truncEnding(sb, 5);
        return sb.toString();
    }
    //不能定义无参构造器，否则fastjson会使用它来创建对象

    public Condition(String key, String operator, String value) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;
    }

    private String key;
    private String operator;
    private String value;
}



